package org.example;

import com.alibaba.fastjson.JSON;
import com.entity.TbClass;
import com.entity.TbStudent;
import com.mapper.TbClassMapper;
import com.mapper.TbStudentMapper;
import com.util.ResponseUtil;
import com.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "tbCacheOne", value = "/zhujie/test")
public class TestServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {


        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        TbStudentMapper tbStudentMapper = sqlSession.getMapper(TbStudentMapper.class);

        // @Select
        List<TbStudent> listTbStudent = tbStudentMapper.listAll();
        System.out.println(JSON.toJSONString(listTbStudent));

        // 参数注解
        TbClassMapper tbClassMapper = sqlSession.getMapper(TbClassMapper.class);
        String classCode = request.getParameter("classCode");
        TbClass tbClass = tbClassMapper.selectTbClassByCode(classCode);
        System.out.println(JSON.toJSONString(tbClass));

//        // @Insert
//        {
//            TbStudent tbStudent = new TbStudent();
//            tbStudent.setClassCode("2301");
//            tbStudent.setStudentCode("aaa");
//            tbStudent.setStudentName("姓名aaa");
//            tbStudentMapper.insert(tbStudent);
//            sqlSession.commit();
//        }

//        // @Update
//        {
//            TbStudent tbStudent = tbStudentMapper.selectByStudentCode("a1");
//            tbStudent.setStudentName(tbStudent.getStudentName() + "1");
//            tbStudentMapper.update(tbStudent);
//            sqlSession.commit();
//        }

        // @Delete
        {
            tbStudentMapper.delete("aaa");
            sqlSession.commit();
        }

        // 解决中文乱码并打印
        ResponseUtil.doPrint(response, JSON.toJSONString(listTbStudent));
    }

    public void destroy() {
    }
}